package vaani.shabd;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Vector;

/**
 *
 * @author sourcemorph
 */

public class ApplicationMiner {

    private static Vector<ArrayList<String>> extension;
    private static ArrayList<String> application;

    static {
        extension = new Vector<ArrayList<String>>();
        application = new ArrayList<String>();
        try {
            FileInputStream fstream = new FileInputStream("data/defaultapplications.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(fstream));
            String temp;
            ArrayList<String> t = new ArrayList<String>();
            boolean flag = true;
            while((temp = reader.readLine()) != null) {
                if(temp.compareTo("##") == 0) {
                    reader.readLine();
                    if(!flag) {
                        extension.add(t);
                        t = new ArrayList<String>();
                    }
                    else
                        flag = false;
                    application.add(reader.readLine());
                }
                else {
                    if(temp.compareTo("##!") == 0) {
                        extension.add(t);
                    }
                    else {
                        t.add(temp);
                    }
                }
            }
            reader.close();
            fstream.close();
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }

    public static String MatchApplication(String s) {
        for(ArrayList<String> a : extension) {
            if(a.contains(s)) {
                return application.get(extension.indexOf(a));
            }
        }
        return null;
    }
}
